package com.storm.future.advisor;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 *
 * 构建一个自定义的切面，从而不再需要使用切面表达式
 *
 * @author gu
 * @date 2020/5/9 14:30
 */
@Component
public class RedAlertAdvisor implements PointcutAdvisor {

    @Override
    public Pointcut getPointcut() {
        return new EngineerPointcut();
    }

    @Override
    public Advice getAdvice() {
        return new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("执行前");
                Method method = invocation.getMethod();
                Object[] arguments = invocation.getArguments();
                Object aThis = invocation.getThis();
                Object invoke = method.invoke(aThis, arguments);
                System.out.println("执行后");
                return invoke;
            }
        };
    }

    @Override
    public boolean isPerInstance() {
        return false;
    }
}
